home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Dialectic 1.2 / source / Dialectic ƒ / Shell ƒ / file interface.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  4.5 KB  |  147 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        file interface.c
  4.  
  5. Purpose:    This module handles the standard file package (open & save
  6.             dialogs) and returns an FSSpec to deal with.
  7.  
  8. \**********************************************************************/
  9.  
  10. #include "Script.h"
  11. #include "file interface.h"
  12. #include "environment.h"
  13. #include "util.h"
  14.  
  15. Boolean GetSourceFile(FSSpec *editFile)
  16. /* a standard procedure which shows an open dialog box and returns the FSSpec of
  17.    the file you selected (or returns FALSE if you cancelled) */
  18. {
  19.     Point                where;
  20.     OSErr                isHuman;
  21.     StandardFileReply    reply;
  22.     SFReply                oldReply;
  23.     FInfo                fndrInfo;
  24.     long                procID;
  25.     int                    i;
  26.     SFTypeList            theTypes;
  27.     
  28.     theTypes[0]='TEXT';
  29.     theTypes[1]='ttro';
  30.     isHuman=FALSE;            /* to err is human... */
  31.     if (gStandardFile58)    /* in system 7 or later, all the work is done for us */
  32.         StandardGetFile(0L, 2, theTypes, &reply);    /* reply's got an FSSpec in it afterwards */
  33.     else    /* pre-system-7, we have to do a bit of work manually */
  34.     {
  35.         where.h = (screenBits.bounds.right - 348)/2;    /* center horizontally */
  36.         where.v = (screenBits.bounds.bottom - 200)/3;    /* 1:3 vertically */
  37.         SFGetFile(where, "\p", 0L, 2, theTypes, 0L, &oldReply);    /* old routine */
  38.         
  39.         reply.sfGood = oldReply.good;
  40.         if (reply.sfGood)    /* if user selected a file */
  41.         {
  42.             reply.sfType = oldReply.fType;    /* type of file */
  43.             isHuman=GetWDInfo(oldReply.vRefNum, &reply.sfFile.vRefNum,    /* get vRefNum */
  44.                                 &reply.sfFile.parID, &procID);            /* and parID */
  45.             if (isHuman!=noErr)
  46.             {
  47.                 reply.sfFile.vRefNum = oldReply.vRefNum;    /* default to old method */
  48.                 reply.sfFile.parID = 0;                        /* & 0 parID -- let the */
  49.             }                                                /* system figure it out */
  50.             
  51.             /* get the name */
  52.             Mymemcpy(reply.sfFile.name, oldReply.fName, oldReply.fName[0]+1);
  53.             
  54.             /* get the finder flags of the selected file -- I've never used them,
  55.                but it's just for completeness; StandardGetFile() will retrieve
  56.                this info, so we should fake it for consistency */
  57.             reply.sfScript=smSystemScript;
  58.             isHuman=HGetFInfo(reply.sfFile.vRefNum, reply.sfFile.parID,
  59.                                 reply.sfFile.name, &fndrInfo);
  60.             reply.sfFlags=(isHuman==noErr) ? fndrInfo.fdFlags : 0;
  61.             
  62.             reply.sfIsFolder=FALSE;
  63.             reply.sfIsVolume=FALSE;
  64.         }
  65.     }
  66.  
  67.     if ((reply.sfGood) && (!isHuman))    /* make the actual FSSpec */
  68.         MyMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name,
  69.                             editFile);
  70.         
  71.     return ((reply.sfGood) && (!isHuman));    /* TRUE if we have a valid file selected */
  72. }
  73.  
  74. Boolean GetDestFile(FSSpec *destFS, Boolean *deleteTheThing, Str255 theTitle)
  75. /* a standard save dialog box -- given a title (for the prompt), it returns the
  76.    file's FSSpec in destFS and whether a file of that name already exists in
  77.    deleteTheThing (TRUE if file already exists) */
  78. /* details are pretty much the same as GetSourceFile(), see above */
  79. {
  80.     Str255                otherName;
  81.     StandardFileReply    reply;
  82.     SFReply                oldReply;
  83.     Point                where;
  84.     FInfo                fndrInfo;
  85.     long                procID;
  86.     OSErr                isHuman, err;
  87.     int                    i;
  88.         
  89.     isHuman=FALSE;
  90.     if (gStandardFile58)
  91.         StandardPutFile(theTitle, destFS->name, &reply);
  92.     else
  93.     {
  94.         where.h = (screenBits.bounds.right - 304)/2;
  95.         where.v = (screenBits.bounds.bottom - 184)/3;
  96.         SFPutFile(where, theTitle, destFS->name, 0, &oldReply);
  97.         reply.sfGood = oldReply.good;
  98.         if (reply.sfGood)
  99.         {
  100.             isHuman=GetWDInfo(oldReply.vRefNum, &reply.sfFile.vRefNum,
  101.                                 &reply.sfFile.parID, &procID);
  102.             if (isHuman!=noErr)
  103.             {
  104.                 reply.sfFile.vRefNum = oldReply.vRefNum;
  105.                 reply.sfFile.parID = 0;
  106.             }            
  107.             
  108.             Mymemcpy(reply.sfFile.name, oldReply.fName, oldReply.fName[0]+1);
  109.             
  110.             reply.sfScript = smSystemScript;
  111.             err=HGetFInfo(reply.sfFile.vRefNum, reply.sfFile.parID,
  112.                                 reply.sfFile.name, &fndrInfo);
  113.             reply.sfReplacing=(err!=fnfErr);
  114.             
  115.             reply.sfIsFolder=FALSE;
  116.             reply.sfIsVolume=FALSE;
  117.         }
  118.     }
  119.  
  120.     if ((reply.sfGood) && (!isHuman))
  121.     {
  122.         *deleteTheThing=reply.sfReplacing;    
  123.         MyMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name,
  124.                         destFS);
  125.     }
  126.     
  127.     return ((reply.sfGood) && (!isHuman));
  128. }
  129.  
  130. pascal OSErr MyMakeFSSpec(short vRefNum, long parID, Str255 fileName,
  131.     FSSpecPtr myFSS)
  132. /* a standard function for creating an FSSpec out of its three component parts.
  133.    if the system supports FSSpecs, we let it do the work; otherwise we have to
  134.    do it manually. */
  135. {
  136.     if (gHasFSSpecs)
  137.         return FSMakeFSSpec(vRefNum, parID, fileName, myFSS);
  138.     else
  139.     {
  140.         myFSS->vRefNum=vRefNum;
  141.         myFSS->parID=parID;
  142.         Mymemcpy(myFSS->name, fileName, fileName[0]+1);
  143.     }
  144.     
  145.     return noErr;
  146. }
  147.